﻿using System;
using System.Windows.Markup;

namespace CWM.Client.Data
{
    public partial class DynamicDataExtension : MarkupExtension
    {
        public enum TypeEnum
        {
            // 查sql
            ExpressModel,
            Role,
            Organization,
            Site,
            ServiceProvider,
            ServiceType,
            SortingRule,
            Price,
            TrackingNetwork,
            Currency,
            Schedule,
            ServiceProviderConnectionMessage,
            User,
            Client
        };

        public bool Cache { get; set; } = false;
        public bool Optional { get; set; } = false;
        public TypeEnum? Type { get; set; }

        public DynamicDataExtension()
        {
        }

        public DynamicDataExtension(TypeEnum type)
        {
            this.Type = type;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (this.Type != null)
            {
                DynamicDataProvider dataProvider = new DynamicDataProvider();
                dataProvider.Cache = this.Cache;
                dataProvider.Optional = this.Optional;
                var propertyInfo = dataProvider.GetType().GetProperty("" + this.Type);
                if (propertyInfo != null)
                {
                    return propertyInfo.GetValue(dataProvider);
                }
                return null;
            }
            else
            {
                return null;
            }
        }
    }
}